package com.hound;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class HelloLucene {

    @Test
    public void index() throws IOException {
        System.out.println("------------< index start >--------------");
        //1.创建 Directory(索引的保存位置：内存或硬盘)
//        Directory directory = new RAMDirectory();
        Directory directory = FSDirectory.open(new File("d:/lucene/index01"));

        //2.创建 IndexWriter
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
        IndexWriter indexWriter = null;
        try {
            indexWriter = new IndexWriter(directory, indexWriterConfig);

            // 3.创建Document
            Document document = null;

            // 4.为 Document 创建 Field
            File file = new File("d:/docs");
            for (File f : file.listFiles()) {
                document = new Document();
                document.add(new Field("content", new FileReader(f)));
                document.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("path", f.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                // 5.通过 IndexWriter 添加文档到索引中
                indexWriter.addDocument(document);
            }
            System.out.println("------------< index end >--------------");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (indexWriter != null) {
                try {
                    indexWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Test
    public void searcher() {
        IndexReader indexReader = null;
        try {
            //1. 创建Directory
            Directory directory = FSDirectory.open(new File("d:/lucene/index01"));
            //2. 创建 IndexReader
            indexReader = IndexReader.open(directory);
            //3. 根据indexReader 创建indexSearcher
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            //4. 创建搜索用的Query
            QueryParser queryParser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
            Query query = queryParser.parse("java");// 确定要搜索的内容
            //5. 根据Searcher 搜索并且返回TopDocs
            TopDocs topDocs = indexSearcher.search(query, 10);//条数
            //6. 根据TopDocs 获取 ScoreDocs
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            for (ScoreDoc scoreDoc : scoreDocs) {
                //7. 根据searcher 和 ScoreDoc 对象获取对应的Document
                Document doc = indexSearcher.doc(scoreDoc.doc);
                //8. 根据Document对象获取对应的值
                System.out.println(doc.get("filename"));
                System.out.println(doc.get("path"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            if (indexReader != null) {
                try {
                    indexReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }
}
